interface ISubscriber<T> {
    (v: T): void;
}
export class BehaviorSubject<T> {
  _value?: T;

  _subcribers: ISubscriber<T>[] = [];

  constructor(v?: T) {
      this._value = v;
  }

  /**
 * 添加事件监听
 * @param name
 * @param call
 */
  subscribe(subcriber: ISubscriber<T>) {
    this._subcribers.push(subcriber);
    return () => {
        const index = this._subcribers.findIndex(s => s === subcriber);
        if (index > -1) {
            this._subcribers.splice(index, 1);
        }
    }
  }

  next(v: T) {
      this._subcribers.forEach(s => s(v));
  }

  getValue() {
      return this._value;
  }
}